﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.Serialization;

namespace QbservableCore.Serialization
{

    [DataContract]
    public class MemberMemberBindingNode : MemberBindingNodeBase
    {
        [DataMember]
        public MemberBindingNodeBase[] MemberBindings { get; set; }

        protected internal override void TraverseChildren(Func<ExpRepresentationBase, ExpRepresentationBase> action)
        {
            foreach (var binding in MemberBindings)
            {
                binding.TraverseChildren(action);
            }
        }

        protected internal override MemberBinding ToMemberBindingCore(ExepressionBuilderCache exepressionBuilderCache, MemberInfo member)
        {
            MemberBinding[] memberBindings = MemberBindings.Select(binding => binding.ToMemberBinding(exepressionBuilderCache)).ToArray();
            return Expression.MemberBind(member, memberBindings);
        }

        public override string ToString()
        {
            return string.Join(", ", (object[])MemberBindings);
        }
    }
}
